<template>
    <div style="background-color: #F2F2F2; padding: 20px;">
        <el-row>
            <el-col :span="4">
                <div class="grid-content title-wrapper" style="margin-bottom: 20px;">
                    <el-breadcrumb separator-class="el-icon-arrow-right">
                        <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
                        <el-breadcrumb-item :to="{ path: '/account-info' }">账号信息</el-breadcrumb-item>
                        <el-breadcrumb-item>修改密码</el-breadcrumb-item>
                    </el-breadcrumb>
                </div>
            </el-col>
        </el-row>

        <el-card class="password-card">
            <el-form :model="passwordForm" :rules="rules" ref="passwordForm" label-width="100px" class="password-form">
                <el-form-item label="原密码" prop="oldPassword">
                    <el-input type="password" v-model="passwordForm.oldPassword" show-password></el-input>
                </el-form-item>
                <el-form-item label="新密码" prop="newPassword">
                    <el-input type="password" v-model="passwordForm.newPassword" show-password></el-input>
                </el-form-item>
                <el-form-item label="确认密码" prop="confirmPassword">
                    <el-input type="password" v-model="passwordForm.confirmPassword" show-password></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="submitForm">确认修改</el-button>
                    <el-button @click="goBack">返回</el-button>
                </el-form-item>
            </el-form>
        </el-card>
    </div>
</template>

<script>
export default {
    data() {
        const validatePass = (rule, value, callback) => {
            if (value === '') {
                callback(new Error('请输入密码'));
            } else {
                if (this.passwordForm.confirmPassword !== '') {
                    this.$refs.passwordForm.validateField('confirmPassword');
                }
                callback();
            }
        };
        const validatePass2 = (rule, value, callback) => {
            if (value === '') {
                callback(new Error('请再次输入密码'));
            } else if (value !== this.passwordForm.newPassword) {
                callback(new Error('两次输入密码不一致!'));
            } else {
                callback();
            }
        };
        return {
            passwordForm: {
                oldPassword: '',
                newPassword: '',
                confirmPassword: ''
            },
            rules: {
                oldPassword: [
                    { required: true, message: '请输入原密码', trigger: 'blur' },
                    { min: 6, message: '密码长度不能小于6位', trigger: 'blur' }
                ],
                newPassword: [
                    { required: true, validator: validatePass, trigger: 'blur' },
                    { min: 6, message: '密码长度不能小于6位', trigger: 'blur' }
                ],
                confirmPassword: [
                    { required: true, validator: validatePass2, trigger: 'blur' }
                ]
            }
        }
    },
    methods: {
        submitForm() {
            this.$axios.post('/api/user/updateAdministratorPassword', this.passwordForm).then(res => {
                if(res.data.code === 200) {
                    this.$message.success('密码修改成功');
                    this.$router.push('/index');
                }
            });
        },
        goBack() {
            this.$router.push('/account-info');
        }
    }
}
</script>

<style scoped>
.password-card {
    max-width: 600px;
    margin: 20px auto;
}

.password-form {
    max-width: 400px;
    margin: 0 auto;
}
</style> 